<?php

/**
 * This is the model class for table "ur_book".
 *
 * The followings are the available columns in table 'ur_book':
 * @property integer $id
 * @property integer $categoryId
 * @property string $name
 * @property string $author
 * @property string $publish
 * @property integer $publish_datetime
 * @property integer $publish_version
 * @property integer $print_datetime
 * @property integer $print_version
 * @property string $ISBN
 * @property integer $words
 * @property integer $pages
 * @property string $format
 * @property string $paper
 * @property string $package
 * @property string $market_price
 * @property string $small_pic
 * @property string $big_pic
 * @property integer $is_recommend
 * @property string $price
 */
class UrBook extends CActiveRecord
{
	
	public $getUrl;
	public $getDesirelevel;
	public $getDesireCount;
	/**
	 * Returns the static model of the specified AR class.
	 * @return UrBook the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ur_book';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			//array('categoryId, name, format, paper, package, market_price, small_pic, price', 'required'),
			array('categoryId, publish_datetime, publish_version, print_datetime, print_version, words, pages, is_recommend', 'numerical', 'integerOnly'=>true),
			array('name, ISBN, paper, package, small_pic, big_pic,author,publish,format', 'length', 'max'=>150),
			array('market_price, price', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, categoryId, name, author, publish, publish_datetime, publish_version, print_datetime, print_version, ISBN, words, pages, format, paper, package, market_price, small_pic, big_pic, is_recommend, price', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'extra'=>array(self::HAS_ONE,'UrBookExtra','id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'categoryId' => 'Category',
			'name' => 'Name',
			'author' => 'Author',
			'publish' => 'Publish',
			'publish_datetime' => 'Publish Datetime',
			'publish_version' => 'Publish Version',
			'print_datetime' => 'Print Datetime',
			'print_version' => 'Print Version',
			'ISBN' => 'Isbn',
			'words' => 'Words',
			'pages' => 'Pages',
			'format' => 'Format',
			'paper' => 'Paper',
			'package' => 'Package',
			'market_price' => 'Market Price',
			'small_pic' => 'Small Pic',
			'big_pic' => 'Big Pic',
			'is_recommend' => 'Is Recommend',
			'price' => 'Price',
		);
	}
	
	public function getUrl(){
		return app()->controller->createUrl('/book/show',array('id'=>$this->id,'title'=>$this->name));
		}
		
	public function getDesirelevel(){
		$d3 = UrVote::model()->count('bid=:bid and desire=:desire',array(':bid'=>$this->book,':desire'=>3));
		$d2 = UrVote::model()->count('bid=:bid and desire=:desire',array(':bid'=>$this->book,':desire'=>2));
		$d1 = UrVote::model()->count('bid=:bid and desire=:desire',array(':bid'=>$this->book,':desire'=>1));
		return 3*d3+2*d2+1*d1;
	}
	
	public function getDesireCount(){
		$data[1]=UrVote::model()->count('desire=:desire and bid=:bid',array(':desire'=>1,':bid'=>$this->id));	
		$data[2]=UrVote::model()->count('desire=:desire and bid=:bid',array(':desire'=>2,':bid'=>$this->id));	
		$data[3]=UrVote::model()->count('desire=:desire and bid=:bid',array(':desire'=>3,':bid'=>$this->id));
		return	$data;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('categoryId',$this->categoryId);

		$criteria->compare('name',$this->name,true);

		$criteria->compare('author',$this->author,true);

		$criteria->compare('publish',$this->publish,true);

		$criteria->compare('publish_datetime',$this->publish_datetime);

		$criteria->compare('publish_version',$this->publish_version);

		$criteria->compare('print_datetime',$this->print_datetime);

		$criteria->compare('print_version',$this->print_version);

		$criteria->compare('ISBN',$this->ISBN,true);

		$criteria->compare('words',$this->words);

		$criteria->compare('pages',$this->pages);

		$criteria->compare('format',$this->format,true);

		$criteria->compare('paper',$this->paper,true);

		$criteria->compare('package',$this->package,true);

		$criteria->compare('market_price',$this->market_price,true);

		$criteria->compare('small_pic',$this->small_pic,true);

		$criteria->compare('big_pic',$this->big_pic,true);

		$criteria->compare('is_recommend',$this->is_recommend);

		$criteria->compare('price',$this->price,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}
